package com.rosense.client.i;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.rosense.client.rpc.RPCBean;
import com.rosense.client.rpc.RPCIoBean;
import com.rosense.commons.DateUtils;
import com.rosense.commons.LangUtils;
import com.rosense.commons.NumberUtils;
import com.rosense.commons.StringsUtils;

/**
 * @author 李岩飞
 * @email eliyanfei@126.com	
 * 2018年1月24日 下午2:22:21
 * 
 */
public class WRIo extends IWriteReader {
	@Override
	public String getName() {
		return "io";
	}

	@Override
	public String getText() {
		return "磁盘";
	}

	@Override
	public void write(String host, RPCBean bean) {
		RPCIoBean ioBean = (RPCIoBean) bean;
		StringBuilder sb = new StringBuilder();
		sb.append(System.currentTimeMillis()).append(",");
		sb.append(ioBean.iousage).append(",");
		sb.append(ioBean.total).append(",");
		sb.append(ioBean.used);
		for (String key : ioBean.diskMap.keySet()) {
			sb.append(",").append(key).append("_").append(ioBean.diskMap.get(key));
		}
		append(host, sb.toString());
	}

	@Override
	protected void parserLine(String line, Map<String, String> map) {
		String[] cols = line.split(",");
		RPCIoBean io = new RPCIoBean();
		io.time = LangUtils.toLong((cols[0]));
		io.iousage = LangUtils.toFloat(cols[1], 0);
		io.total = LangUtils.toLong(cols[2], 0);
		io.used = LangUtils.toLong(cols[3], 0);
		map.put("time", DateUtils.formatDate(new Date(io.time), "yyyyMMddHHmm"));
		map.put("value", "使用率:" + NumberUtils.formatNumber("0.0%", io.used, io.total)+",繁忙程度:"+io.iousage+"%");
	}
	@Override
	protected void parserLineToMap(String line,Map<String,List<RPCBean>> map) {
			String[] cols = line.split(",");
			RPCIoBean io = new RPCIoBean();
			io.time = LangUtils.toLong((cols[0]));
			io.iousage = LangUtils.toFloat(cols[1], 0);
			io.total = LangUtils.toLong(cols[2], 0);
			io.used = LangUtils.toLong(cols[3], 0);
			String time = DateUtils.formatDate(new Date(io.time), "HH");
			List<RPCBean> list = map.get(time);
			if(list==null){
				list = new ArrayList<RPCBean>();
				map.put(time, list);
			}
			list.add(io);
	}
	@Override
	protected void getAvgInfo(Map<String, List<RPCBean>> mapInfo,Map<String,String> map,double limit){
		float total1 = 0;
		float total2 = 0;
		float totalNum = 0;
		String str1 = "";
		String str2 = "";
		String data1 = "";
		String data2 = "";
		String xdata = "";
		int n = 0;
		for(String time : mapInfo.keySet()){
			List<RPCBean> list = mapInfo.get(time);
			totalNum += list.size();
			float value1 = 0;
			float value2 = 0;
			for(int i=0;i<list.size();i++){
				RPCIoBean io = (RPCIoBean) list.get(i);
				value1 += LangUtils.toFloat(NumberUtils.formatNumber("0.00", io.used*100, io.total),0);
				value2 += LangUtils.toFloat(io.iousage,0);
			}
			total1 += value1;
			total2 += value2;
			value1 = (float)NumberUtils.formatFmZero(value1,list.size());
			value2 = (float)NumberUtils.formatFmZero(value2,list.size());
			data1 +=value1;
			data2 +=value2;
			xdata +=time;
			if(value1>limit){
				if(StringsUtils.isBlank(str1)){
					str1 += "使用率:";
				}else{
					str1 += ",";
				}
				str1 += time;
			}
			if(value2>limit){
				if(StringsUtils.isBlank(str2)){
					str2 += "<br/>繁忙程度:";
				}else{
					str2 += ",";
				}
				str2 += time;
			}
			if(n++<mapInfo.size()-1){
				data1 += ",";
				data2 += ",";
				xdata += ",";
			}
		}
		if(StringsUtils.isNotBlank(str1)||StringsUtils.isNotBlank(str1)){
			map.put("hour", str1+str2);
			map.put("day", "使用率："+ NumberUtils.formatNumber("0.00%", total1,totalNum)+",繁忙程度:"+NumberUtils.formatNumber("0.00%", total2,totalNum));
		}
		//历史的折线图
		if(limit<0){
			map.put("name1", "磁盘使用率");
			map.put("name2", "磁盘繁忙程度");
			map.put("xdata", xdata);
			map.put("data1", data1);
			map.put("data2", data2);
		}
	}
	
	@Override
	public List<? extends RPCBean> read(String host) {
		List<RPCIoBean> list = new ArrayList<RPCIoBean>();
		String value = readLastLine(host);
		String[] values = value.trim().split("\n");
		for (String line : values) {
			String[] cols = line.split(",");
			RPCIoBean io = new RPCIoBean();
			io.time = LangUtils.toLong((cols[0]));
			io.iousage = LangUtils.toFloat(cols[1], 0);
			io.total = LangUtils.toLong(cols[2], 0);
			io.used = LangUtils.toLong(cols[3], 0);
			io.diskMap = getDiskMap(cols);
			list.add(io);
		}
		return list;
	}

	public Map<String, String> getDiskMap(String[] cols) {
		Map<String, String> map = new HashMap<String, String>();
		for (int i = 4; i < cols.length; i++) {
			String value = cols[i];
			String[] values = value.split("_");
			map.put(values[0], values[1] + "_" + values[2]);
		}
		return map;
	}
}
